package com.github.poplar.monitortoolcommon.aop;

import com.github.poplar.monitortoolcommon.aop.annotation.LogDescribe;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @description:
 * @author: hkai
 * @date: 2022/1/10 21:30
 * @version: 1.0
 */
@Slf4j
@Aspect
public class LogDescribeAspectJ {

    @Pointcut(value = "@annotation(com.github.poplar.monitortoolcommon.aop.annotation.LogDescribe)")
    public void logs(){}

    @Around("logs()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Class declaringType = signature.getDeclaringType();
        Method method = signature.getMethod();
        LogDescribe annotation = method.getAnnotation(LogDescribe.class);
        log.info("方法功能描述 description: {}", annotation.desc());
        Object[] args = point.getArgs();
        log.info("方法请求路径 method: {}.{}", declaringType.getName(), method.getName());
        for (Object obj : args) {
            log.info("方法 {}.{} 入参: {}", declaringType.getSimpleName(), method.getName(), obj);
        }
        Object ret = point.proceed();
        log.info("方法 {}.{}, 执行结果: {}", declaringType.getSimpleName(), method.getName(), ret);
        return ret;
    }
}
